package net.learn.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.convert.SysAboutConvert;
import net.learn.system.entity.SysAboutEntity;
import net.learn.system.query.SysAboutQuery;
import net.learn.system.service.SysAboutService;
import net.learn.system.vo.SysAboutVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
* 关于我们
*/
@RestController
@RequestMapping("sys/about")
@Tag(name="关于我们")
@AllArgsConstructor
public class SysAboutController {
    private final SysAboutService sysAboutService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<SysAboutVO>> page(@Valid SysAboutQuery query){
        PageResult<SysAboutVO> page = sysAboutService.page(query);

        return Result.ok(page);
    }

    @PostMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('sys:about:list')")
    public Result<List<SysAboutVO>> list(@RequestBody @Valid SysAboutQuery query){
        List<SysAboutVO> list = sysAboutService.getList(query);

        return Result.ok(list);
    }
    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:about:info')")
    public Result<SysAboutVO> get(@PathVariable("id") Long id){
        SysAboutEntity entity = sysAboutService.getById(id);

        return Result.ok(SysAboutConvert.INSTANCE.convert(entity));
    }
    @GetMapping("get")
    @Operation(summary = "详细信息")
    @PreAuthorize("hasAuthority('sys:about:get')")
    public Result<SysAboutVO> get(@Valid SysAboutQuery query){
        SysAboutEntity entity = sysAboutService.getById(query.getId());

        return Result.ok(SysAboutConvert.INSTANCE.convert(entity));
    }
    @PostMapping("save")
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody SysAboutVO vo){
        sysAboutService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:about:update')")
    public Result<String> update(@RequestBody @Valid SysAboutVO vo){
        sysAboutService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList){
        sysAboutService.delete(idList);

        return Result.ok();
    }
}